Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Public Class NewItemDetail

    Public ID As Integer
    Public Action As String

    Private Sub NewItemDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadIcon(Me)
        'Load Item Name and Unit Price

        LoadDataToLookUpEdit("SELECT * FROM Users WHERE Status = 1", "ID", "UserName", "Staff", Me.cboUser)
        Me.cboUser.EditValue = CurrUserID

        Dim str As String
        Select Case Action
            Case "ADD"
                str = "SELECT * FROM Items WHERE ID = " & ID
            Case "EDIT"
                Exit Sub
        End Select

        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            Me.lblItemName.Text = dr.Item("ItemName").ToString
            Me.txtUnitPrice.Text = dr.Item("UnitPrice").ToString
            Me.txtQuantity.Text = 1
            Dim arrPic() As Byte
            Try
                If Not dr.Item("Picture") Is Nothing Then
                    arrPic = dr.Item("Picture")
                    Dim mmPicture As New MemoryStream(arrPic)
                    Me.PictureEdit1.Image = Image.FromStream(mmPicture)
                End If
            Catch ex As Exception
            End Try
        End While
        dr.Close()

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click


        If Me.txtQuantity.Text = "" Or IsNumeric(Me.txtQuantity.Text) = False Then
            MsgBox("Please Fill In Quantity", MsgBoxStyle.Exclamation)
            Me.txtQuantity.Focus()
            Exit Sub
        End If

        If Me.txtUnitPrice.Text = "" Or IsNumeric(Me.txtUnitPrice.Text) = False Then
            MsgBox("Please Fill In Unit Price", MsgBoxStyle.Exclamation)
            Me.txtUnitPrice.Focus()
            Exit Sub
        End If

        If Me.cboUser.EditValue = Nothing Then
            MsgBox("Please Fill In Service By", MsgBoxStyle.Exclamation)
            Me.cboUser.Focus()
            Exit Sub
        End If

        FGT_NewItemDetail_ItemQty = Me.txtQuantity.Text
        FGT_NewItemDetail_ItemUnitPrice = Me.txtUnitPrice.Text
        FGT_NewItemDetail_ServiceBy = Me.cboUser.EditValue

        Me.Close()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()

    End Sub

    Private Sub txtUnitPrice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUnitPrice.KeyDown
      
    End Sub

    Private Sub txtQuantity_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtQuantity.KeyDown
        If e.KeyCode = Keys.Enter Then
            If Me.txtQuantity.Text = "" Or IsNumeric(Me.txtQuantity.Text) = False Then
                MsgBox("Please Fill In Quantity", MsgBoxStyle.Exclamation)
                Me.txtQuantity.Focus()
                Exit Sub
            End If

            If Me.txtUnitPrice.Text = "" Or IsNumeric(Me.txtUnitPrice.Text) = False Then
                MsgBox("Please Fill In Unit Price", MsgBoxStyle.Exclamation)
                Me.txtUnitPrice.Focus()
                Exit Sub
            End If

            FGT_NewItemDetail_ItemQty = Me.txtQuantity.Text
            FGT_NewItemDetail_ItemUnitPrice = Me.txtUnitPrice.Text
            FGT_NewItemDetail_ServiceBy = Me.cboUser.EditValue

            Me.Close()
        End If
    End Sub
End Class